package com.example.xxpk.controller.useroutifts;

import com.example.xxpk.bo.OutfitInfo;
import com.example.xxpk.common.result.R;
import com.example.xxpk.service.useroutifts.IUserOutiftsService;
import com.example.xxpk.vo.UserOutfitsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangguihong
 * @since 2025-02-13
 */
@RestController
@RequestMapping("/userOutifts")
@RequiredArgsConstructor
@Api(tags="用户装扮表")
public class UserOutiftsController {

    private final IUserOutiftsService userOutiftsService;

    // 获取可用的装扮信息
    @GetMapping("/getOutfitsInfo")
    @ApiOperation("获取可用的装扮信息")
    public R getOutfitInfo(@RequestParam Integer userId, @RequestParam String category) {
        List<OutfitInfo> outfitInfoList = userOutiftsService.getOutfitInfoByUserIdAndCategory(userId, category);
        return R.data("获取装扮信息成功",outfitInfoList);
    }


    // 使用装扮
    @PostMapping("/use")
    @ApiOperation("使用装扮")
    public R useOutfit(@RequestBody UserOutfitsVo param) {
        try {
            Integer userId = param.getUserId();
            Integer outiftsId = param.getOutiftsId();
            Integer category = param.getCategory();

            // 检查用户是否拥有该装扮
            if (!userOutiftsService.isUserOwnOutfit(userId, outiftsId)) {
                return R.error("您未拥有该装扮，无法使用");
            }
            // 先将同类别其他装扮的 is_using 置为 0
            userOutiftsService.unuseOtherOutfits(userId, category);

            // 再将当前装扮的 is_using 置为 1
            boolean success = userOutiftsService.useOutfit(userId, outiftsId);
            if (success) {
                return R.ok("使用装扮成功");
            } else {
                return R.error("使用装扮失败，请稍后重试");
            }
        } catch (Exception e) {
            return R.error("系统出现异常，请联系管理员");
        }
    }

    // 试玩装扮
    @PostMapping("/trial")
    @ApiOperation("试玩装扮")
    public R trialOutfit(@RequestParam Integer userId, @RequestParam Integer outiftsId) {
        Integer category = userOutiftsService.canTrialOutfit(userId, outiftsId);
        if (category!=null &&category!=0) {
            userOutiftsService.recordTrialOutfit(userId, outiftsId,category);
            return R.ok("试玩成功");
        } else {
            return R.error("不可试玩");
        }
    }

    // 试玩结束
    @PostMapping("/trialEnd")
    @ApiOperation("试玩装扮结束")
    public R trialEndOutfit(@RequestParam Integer userId, @RequestParam Integer outiftsId) {
            userOutiftsService.recordTrialOutOutfit(userId, outiftsId);
            return R.ok("试玩结束");
    }

    @GetMapping("/specific")
    @ApiOperation("用户具体装扮信息")
    public R getSpecificOutfitInfo(@RequestParam Integer userId, @RequestParam Integer outiftsId) {
        OutfitInfo outfitInfo = userOutiftsService.getOutfitInfoByUserIdAndOutiftsId(userId, outiftsId);
        if (outfitInfo != null) {
            return R.data("获取装扮信息成功",outfitInfo);
        } else {
            return R.error("未找到该用户的此装扮信息");
        }
    }
}